home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / mail / pine3.96.tar.gz / pine3.96.tar / pine3.96 / pine / osdep / readfile.os2 < prev    next >
Text File  |  1996-03-29  |  894b  |  42 lines

  1. #line 2 "osdep/readfile.os2"
  2. /*----------------------------------------------------------------------
  3.     Read whole file into memory
  4.  
  5.   Args: filename -- path name of file to read
  6.  
  7.   Result: Returns pointer to malloced memory with the contents of the file
  8.           or NULL
  9.  
  10. This won't work very well if the file has NULLs in it and is mostly
  11. intended for fairly small text files.
  12.  ----*/
  13.  
  14. char *
  15. read_file(filename)
  16.     char *filename;
  17. {
  18.   int   nb;
  19.   char  *buf =NULL;
  20.   char  *p = tmp_20k_buf;
  21.   char  tmp[1024];
  22.   FILE  * fp = fopen(filename, "r");
  23.  
  24.   if (fp){
  25.  
  26.     while (fgets(tmp, sizeof tmp - 1, fp)!=NULL) {
  27.       int len = strlen(tmp);
  28.       if (p - tmp_20k_buf + len > 20480)
  29.         break;
  30.         memcpy(p, tmp, len);
  31.         p += len;
  32.       }
  33.     *p++ = '\0';
  34.     nb = p - tmp_20k_buf;
  35.     fclose(fp);
  36.     buf = fs_get(nb);
  37.     memcpy(buf, tmp_20k_buf, nb);
  38.   }
  39.   return buf;
  40. }
  41.  
  42.